// pages/Goods/goodsOrder/index.js
var common = require("../../../public.js");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: [],
    token: '',
    orderList: [],
    status: -1,
    not: true,
    have: false,
    pageNum: 1,
    active: 0,
    noMoreData: true,
    currentIndex: 0,
    isLoadingBusy: false,
    type: 1,
    refreshing: false,
   
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.type = options.type
    const that = this
    common.getLocalUserInfo(function (success, data) {
      console.log(data,"111111")
      if (success) {
        console.log(data,"111111")
        that.setData({
          userInfo: data.user,
          token: data.token,
          openid: data.openid,
          type: options.type
        }, function () {
          // 1为普通商品订单
          // if (options.type == 1) {
            that.reloadData()
          // }
        })
      }
    }, true)
  },
  // 删除按钮点击事件
  onDelete(e) {
    const orderId = e.currentTarget.dataset.item.orderId
    const that = this
    common.showAlertView('提示', '是否删除该订单？', function (res) {
      if (res) {
        that.submitDelete(orderId)
      }
    })
  },
  // 提交删除事件
  submitDelete(orderId) {
    const that = this
    common.post('/order/delete', {
      orderId,
      userId: that.data.userInfo.id,
      token: that.data.token
    },
      function (res) {
        if (res.status == 200) {
          common.showToast('删除成功！')
          that.reloadData()
        } else {
          common.showRequestErrMsg(res)
        }
      })
  },
  onChange(event) {
    const index = event.detail.name
    this.setData({
      status:index
    })
    this.reloadData(true)
  },
  toStore(e) {
    common.gotoStore(e.currentTarget.dataset.item)
  },
  toComment(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/crowd/crowdCommnet/crowdCommnet?orderId=' + item.orderNo,
    })
  },
  cancelOrder(e) { // 取消订单
    const that = this
    common.showAlertView('提示', '是否取消该订单', function (res) {
      if (res) {
        let orderId = e.currentTarget.dataset.item.orderNo
        let tempJson = {
          userId: that.data.userInfo.id,
          token: that.data.token,
          orderNo: orderId
        }
        common.post('/crowdfunding/closeOrder', tempJson, function (res) {
          if (res.status === 200) {
            common.showToast('已取消订单')
            that.reloadData()
          } else {
            common.showRequestErrMsg(res)
          }
        })
      }
    })
  },
  payNow(e) {
    const item = e.currentTarget.dataset.item
    const that = this
    const tempJson = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderId: item.id,
      openid: that.data.openid,
    }

  
    common.wxPayRequest('/wxxcxPayForCrowdfundingGoods', tempJson, function (res) {
    
      if (res.status === 200) {
        //微信支付
        wx.requestPayment({
          'timeStamp': res.data.timeStamp,
          'nonceStr': res.data.nonceStr,
          'package': res.data.package,
          'signType': res.data.signType,
          'paySign': res.data.paySign,
          'success': function (res) {
            common.showToast('支付成功', 'success')
            that.reloadData()
          },
          'fail': function (res) {
            console.log('微信支付res为',res);
            if (res.errMsg === 'requestPayment:fail cancel') {
            } else {
              common.showToast(res.errMsg)
            }
          }
        })
      }
    })
  },
  // 确认收货
  confirm(e) {
    const item = e.currentTarget.dataset.item
    const that = this
    const param = {
      userId: that.data.userInfo.id,
      token: that.data.token,
      orderNo: item.orderNo
    }
    common.post('/crowdfunding/confirmOrder', param, function (res) {
      if (res.status === 200) {
        common.showToast('确认收货成功', 'success')
        that.reloadData()
      } else {
        common.showRequestErrMsg(res)
      }
    })
  },
  /**
   * 获取订单列表
   * @param {boolean} refresh 是否刷新列表数据
   */
  getOrderList(refresh = false) {
    if (this.data.isLoadingBusy === true) {
      return
    }
    if (refresh) {
      // 这里要区分是加载新的tab数据还是刷新数据
      // 加载新的tab数据不显示下拉刷新，下拉刷新才需要显示
      this.setData({
        noMoreData: false,
        isLoadingBusy: true,
        refreshing: true
      })
    } else {
      if (this.data.noMoreData === true) {
        return
      }
      this.setData({
        isLoadingBusy: true
      })
    }

    const pageNum = refresh ? 1 : this.data.pageNum + 1
    const that = this
   
    var url 
    if(that.data.type==1){
      console.log('商品订单楼')
       var tempJson = {
      userId: that.data.userInfo.id,
      status: that.data.status==-1 ? '' :that.data.status,
      token: that.data.token,
      pageNum
    }
      url= '/order/list'
    }else {
      console.log('团购订单楼')
      console.log(that.data.status,"ceshihcei")
      var tempJson = {
        userId: that.data.userInfo.id,
        status: that.data.status==-1 ? '' :that.data.status,
        token: that.data.token,
        pageNum,
        count:10
      }
      url= '/crowdfundingOrder/getList'
    }

    common.post(url, tempJson, function (res) {
      if (res.status == 200) {
        that.data.pageNum = pageNum
        let orderList = []
        if (res.data.result_list === null || res.data.result_list.length === 0) {
          that.setData({
            noMoreData: true
          })
        } else {
          // for (var i = 0; i < res.data.result_list.length; i++) {
            orderList=res.data.result_list
          // }
          that.setData({
            orderList: refresh ? orderList : [...that.data.orderList, ...orderList]
          },()=>{
            console.log(that.data.orderList)
          })
        }
      } else if (res.msg == '您还没有订单') {
        that.setData({
          noMoreData: true
        })
      } else {
        common.showRequestErrMsg(res)
      }
    }, function () {
      that.setData({
        isLoadingBusy: false
      })
      if (refresh) {
        that.setData({
          refreshing: false
        })
      }
    })
  },
  loadMore() {
    this.getOrderList()
  },
  /**
   * 加载tab页数据
   */
  reloadData() {
    this.setData({
      orderList: [],
    }, _ => {
      this.getOrderList(true)
    })
  },
  toDetail(e) {
    const item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '/pages/crowd/OrderListDetail/OrderListDetail?orderId=' + item.orderNo
      + '&flags=1' 
    })
  },
})